package mysql_sync

// mysql 表结构同步同步

import (
	"core/mysql"
	"core/utils"
	"db"
	"db/stat"
)

const (
	DBPlayer               = "player"
	DBOpenId               = "open_id_data"
	DBAdminPlayer          = "admin_player"
	DBAdminPictureOriginal = db.PicOriginalTable
	DBAdminPictureLove     = db.PicLoveTable
	DBAdminPictureConsider = db.PicConsiderTable
	DBPictureOnline        = db.PicOnlineTable
	DBPictureUpload        = db.PicConsiderTable
	DbStat                 = stat.StatTable
)

var TablesMap = map[string]interface{}{
	DBPlayer:               &db.Player{},
	DBOpenId:               &db.OpenIdData{},
	DBAdminPlayer:          &db.AdminPlayer{},
	DBAdminPictureOriginal: &db.PicOriginalMysql{},
	DBAdminPictureLove:     &db.PicLoveMysql{},
	DBAdminPictureConsider: &db.PicConsiderMysql{},
	DBPictureOnline:        &db.PicOnline{},
	DbStat:                 &stat.Stat{},
}

func SyncTables() (err error) {
	for _, model := range db.Models {
		tableName, tableStructure := model.GetTable()
		TablesMap[tableName] = tableStructure
	}

	syncTables := make([]interface{}, 0)
	for _, tableInfo := range TablesMap {
		syncTables = append(syncTables, tableInfo)
	}
	err = mysql.X.Sync2(syncTables...)
	if err != nil {
		return
	}

	for tableName, tableInfo := range TablesMap {
		var exist bool
		exist, err = mysql.X.IsTableExist(tableName)
		if err != nil {
			return
		}
		if !exist {
			err = mysql.X.CreateTables(tableInfo)
			if err != nil {
				return
			}
			utils.Info(tableInfo)
		}
	}

	return
}
